package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.MyUtil;
import com.yazi.mxz.common.db.dao.CompanyDao;
import com.yazi.mxz.common.db.dao.MemberDao;
import com.yazi.mxz.common.db.dao.MemberPrepaidDao;
import com.yazi.mxz.common.db.dao.MemberPromoteDao;
import com.yazi.mxz.common.db.dao.MemberVipDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.MemberPrepaid;
import com.yazi.mxz.common.entity.beans.MemberPromote;
import com.yazi.mxz.common.entity.beans.MemberVip;
import com.yazi.mxz.common.entity.beans.MemberVipId;
import com.yazi.mxz.common.entity.custom.MemberPrepaid4Search;
import com.yazi.mxz.common.entity.enums.MemberPromoteType;
import com.yazi.mxz.common.entity.enums.MemberVipStatus;
import com.yazi.mxz.common.service.MemberPrepaidService;

@Service("memberPrepaidService")
public class MemberPrepaidServiceImpl extends BasicService<MemberPrepaid> implements MemberPrepaidService {
	private static Logger log = LoggerFactory.getLogger(MemberPrepaidServiceImpl.class);
	
	@Resource
	MemberPrepaidDao memberPrepaidDao;
	@Resource
	MemberDao memberDao;
	@Resource
	MemberVipDao memberVipDao;
	@Resource
	CompanyDao companyDao;
	@Resource
	MemberPromoteDao memberPromoteDao;
	
	@Override
	protected Dao<MemberPrepaid> getDao() {
		return memberPrepaidDao;
	}
	
	@Override
	public MemberPrepaid add(MemberPrepaid memberPrepaid) {
		log.debug("add: memberId={},storeId={},prepaid={}", 
				new Object[]{memberPrepaid.getMemberByMemberId().getMemberId(), memberPrepaid.getStore().getStoreId(),memberPrepaid.getPrepaid()});
		
		// 充值金额
		float prepaid = memberPrepaid.getPrepaid();
		
		Company company = memberPrepaid.getCompany();
		company = companyDao.get(company.getCompanyId());
		
		// 更改用户余额
		MemberVipId  mvId = new MemberVipId();
		mvId.setCompanyId(company.getCompanyId());
		mvId.setMemberId(memberPrepaid.getMemberByMemberId().getMemberId());
		MemberVip mv = memberVipDao.get(mvId);
		
		if (null != mv) {
			// 记录充值前账户余额
			memberPrepaid.setBalance(mv.getBalance());
			
			// 更新企业会员
			
			mv.setBalance(MyUtil.numRoundDown(mv.getBalance() + prepaid, 2).floatValue());
			mv.setPrepaidTotal(MyUtil.numRoundDown(mv.getPrepaidTotal() + prepaid, 2).floatValue());
			if (mv.getPrepaidMax() < prepaid) {
				mv.setPrepaidMax(MyUtil.numRoundDown(prepaid, 2).floatValue());
			}
			mv.setUpdateTime(DateUtil.getTimeStamp());
			memberVipDao.merge(mv);
			
		} else {
			// 记录充值前账户余额
			memberPrepaid.setBalance(0f);
			
			float balance = MyUtil.numRoundDown(prepaid, 2).floatValue();
			
			// 添加企业会员
			
			mv = new MemberVip();
			mv = ValueFactory.validate(mv);
			mv.setId(mvId);
			mv.setBalance(balance);
			mv.setPrepaidTotal(balance);
			mv.setPrepaidMax(balance);
			mv.setCheckCode("" + 12345);// TODO 校验码
			mv.setStatus(MemberVipStatus.NORMAL.getValue());
			mv.setAddTime(DateUtil.getTimeStamp());
			mv.setUpdateTime(DateUtil.getTimeStamp());
			memberVipDao.persist(mv);
		}
		
		// 计算充值优惠
		int sw = company.getMemPromote();// 促销开关(switch)
		boolean isPro = false;// 本次充值是否获得任何优惠
		StringBuffer proData = new StringBuffer();// 本次充值所获得的优惠信息
		float given = 0;// 本次充值所获得的赠送金额
		MemberPromote pro;
		float checkPrepaid = 0;
		MemberPromoteType[] types = MemberPromoteType.values();
		for (MemberPromoteType type : types) {
			
			if ((sw & (1 << type.ordinal())) == 0) {
				continue;
			}
			
			switch(type) {
			case NOW:
				checkPrepaid = prepaid;
				break;
			case TOTAL:
				checkPrepaid = mv.getPrepaidTotal();
				break;
			case MAX:
				checkPrepaid = mv.getPrepaidMax();
				break;
			}
			
			pro = memberPromoteDao.oneFloorPrepaid(company.getCompanyId(), type.ordinal(), checkPrepaid);
			if (null == pro) {
				continue;
			}
			
			if (isPro) {
				proData.append(",");
			} else {
				proData.append("[");
			}
			proData.append("{");
			proData.append("type: "+ pro.getType() +",");
			proData.append("prepaid: "+ pro.getPrepaid() +",");
			proData.append("give: "+ pro.getGive() +",");
			proData.append("}");
			isPro = true;
			
			switch(type) {
			case NOW:
			case TOTAL:
				given += pro.getGive();
				break;
			case MAX:
				// TODO 充值打折优惠处理
				// ...
				break;
			}
		}
		if (isPro) {
			proData.append("]");
		}
		
		// 账户中加上赠送的金额
		if (given > 0) {
			mv.setBalance(MyUtil.numRoundDown(mv.getBalance() + given, 2).floatValue());
			memberVipDao.merge(mv);
		}
		
		// 添加到充值历史记录
		memberPrepaid = ValueFactory.validate(memberPrepaid);
		memberPrepaid.setGiven(MyUtil.numRoundDown(given, 2).floatValue());
		memberPrepaid.setPromote(proData.toString());
		memberPrepaid.setAddTime(DateUtil.getTimeStamp());
		memberPrepaid.setPrepaidId(null);
		return memberPrepaidDao.persist(memberPrepaid);
	}

	@Override
	public Page<MemberPrepaid> list(MemberPrepaid4Search mp4s, Searching si) {
		log.debug("search：MemberPrepaid4Search");
		return memberPrepaidDao.list(mp4s, si);
	}

}
